package app.android.com.wuandroidapp.test.view.definedview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.support.annotation.Px;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Scroller;

import com.nineoldandroids.view.ViewHelper;

import app.android.com.wuandroidapp.R;

/**
 * Created by admin on 2017/6/20.
 */

public class CircleView extends View {

    private int color = Color.RED;
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Scroller scroller;
    public CircleView(Context context) {
        super(context);
        scroller=new Scroller(context);
        init();
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleViews);
        color = a.getColor(R.styleable.CircleViews_circle_color, Color.BLUE);
        a.recycle();
        init();
    }

    private void init() {
        paint.setColor(color);
    }

    int mLastX=0;
    int mLastY=0;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x=(int)event.getRawX();
        int y=(int)event.getRawY();
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:{
                break;
            }
            case MotionEvent.ACTION_MOVE:{
               int deltaX=x-mLastX;
                int deltaY=y-mLastY;
                int translationX= (int)ViewHelper.getTranslationX(this)+deltaX;
                int translationY=  (int) ViewHelper.getTranslationY(this)+deltaY;
                ViewHelper.setTranslationX(this,translationX);
                ViewHelper.setTranslationY(this,translationY);
                break;
            }
            case MotionEvent.ACTION_UP:{

                break;
            }
            default:
                break;
        }
        mLastX=x;
        mLastY=y;
        return true;
    }




    @Override
    public void scrollTo(@Px int x, @Px int y) {
        super.scrollTo(x, y);
    }

    @Override
    public void scrollBy(@Px int x, @Px int y) {
        super.scrollBy(x, y);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSpecMode=MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize=   MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode=MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize=   MeasureSpec.getSize(heightMeasureSpec);
        if(widthSpecMode==MeasureSpec.AT_MOST && heightSpecMode==MeasureSpec.AT_MOST){
            setMeasuredDimension(200,200);
        }else if(widthSpecMode==MeasureSpec.AT_MOST){
            setMeasuredDimension(200,heightSpecSize);
        }else if(heightSpecMode==MeasureSpec.AT_MOST){
            setMeasuredDimension(widthSpecSize,200);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        final int paddingleft = getPaddingLeft();
        final int paddingright = getPaddingRight();
        final int paddingtop = getPaddingTop();
        final int paddingbottom = getPaddingBottom();
        int width = getWidth() - paddingleft - paddingright;
        int height = getHeight() - paddingtop - paddingbottom;
    /*    int width = getWidth();
        int height = getHeight();*/
        int radius = Math.min(width, height) / 2;
        canvas.drawCircle(paddingleft + width / 2, paddingtop + height / 2, radius, paint);
        // canvas.drawCircle(width / 2, height / 2, radius, paint);
    }
}
